using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Net.Sockets;
using Chat.Util;

namespace Chat.Server
{
    public partial class ServerSend : Form
    {
        User u;
        MainServer ms;
        
        public ServerSend()
        {
            InitializeComponent();
        }

        public ServerSend(MainServer ms,User u,ToMsg tm)
        {
            InitializeComponent();
            this.u = u;
            this.ms = ms;
            string formId = tm.toId;
            if (!String.IsNullOrEmpty(formId))
            {
                this.labUserName.Text = Texts.getTitle(tm);
                this.Name = formId;
                this.Text = Texts.getTitle(tm);
            }
        }

        public ServerSend(MainServer ms, User u, FromMsg fm)
        {
            InitializeComponent();
            this.u = u;
            this.ms = ms;
            string formId = fm.fromId;
            if (!String.IsNullOrEmpty(formId))
            {
                this.labUserName.Text = Texts.getTitle(fm);
                this.Name = formId;
                this.Text = Texts.getTitle(fm);
                this.AppendFromMsg(fm);
            }
        }

        public ServerSend(MainServer ms, User u)
        {
            InitializeComponent();
            this.u = u;
            this.ms = ms;           
        }

        public void AppendFromMsg(FromMsg fm)
        {
            string str = Texts.getRecvString(fm);
            AppendText(str);
        }

        public void AppendText(string msg) {
            this.tbShowMsg.AppendText(msg + "\r\n");
        }

        public void SetText(string msg)
        {
            this.tbShowMsg.Text = msg + "\r\n";
        }

        public void ClearText()
        {
            this.tbShowMsg.Text = "";
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string msg = this.tbInputMsg.Text;
            string strMsg = Texts.getSendString(ms.serverUser, msg);
            AppendText(strMsg);
            this.tbInputMsg.Text = "";
            Socket s = u.socket;
            ChatServerAsync.Send(msg, s);
        }

        private void ServerSend_Activated(object sender, EventArgs e)
        {
            this.tbInputMsg.Focus();
        }

        private void ServerSend_FormClosed(object sender, FormClosedEventArgs e)
        {
            ms.htForm.Remove(this.Name);
        }

    }
}